<?php
/**
 * 产品详情控制层
 * 
 * @copyright 	Copyright (c) 2011 thinkapk.com All rights reserved.
 * @package 	ThinkAPK_Action
 * @link 		http://www.thinkapk.com/
 * @author 		richie <richbossman@163.com>
 * @version 	$Id: ProductAction.class.php 7647 2011-06-04 08:56:45Z yemusheng $
 * 
 */
class ProductAction extends ThinkAPK_Action_Mall {
	
	public function index(){
		
		$goods_id=is_numeric($_GET['id'])?$_GET['id']:0;
		if(!$goods_id) $this->error('商品ID不正确');
		$goods=D('Goods')->getGood($goods_id);
		if(!$goods){
			$this->error('很抱歉！您要访问的商品不存在，或已删除！');
		}
		$now_time=time();
		
		//如果是限时促销商品,且没结束，则计算结束时间与当前时间的时差
		if($goods['is_promote']==1){
			if($now_time>=$goods['promote_start_date'] && $now_time<$goods['promote_end_date']){
				//抢购剩余时间
				$goods['diff_promote']=$goods['promote_end_date']-$now_time;
			}else{
				//如果限时促销已结束，或没开始，则隐藏促销信息；
				$goods['is_promote']=0;
			}
		}
		
		//取会员等级折扣信息
		$UR=new UserRankModel();
		$UR->getUserRank();
		$this->assign('user_rank_points',$UR->user_rank_points);
		$this->assign('user_rank_discount',$UR->user_rank_discount);
		unset($UR);
		
		//取商品相册
		$images=D('GoodsGallery')->getGoodsGallery($goods_id);
		$this->assign('pictures',$images);
		unset($images);
		
		//用户评论总概：几星、总评论人数
		$review_total=D('GoodsReviews')->getReviewTotal($goods_id);
		if($review_total) $this->assign('review_total',$review_total);
		
		//取商品相关赠品
		$gifts=D('GiftList')->getGifts($goods_id);
		if(!empty($gifts)) $this->assign('gifts',$gifts);
		unset($gifts);
		
		//商品相关专题
		$special=D('Special')->getGoodsSpec($goods_id);
		if($special) $this->assign('specials',$special);
		unset($special);
		
		//商品属性
		$attrs=D('GoodsAttr')->getGoodsAttr($goods_id);
		if($attrs) $this->assign('attrs',$attrs);
		unset($attrs);
		
		//优惠组合套装
		$goods_group=D('GroupGoods')->getGoodsGroup($goods_id);
		if($goods_group) $this->assign('goods_group',$goods_group);
		unset($goods_group);
		
		//分期付款银行信息
		if($goods['is_fq']){
			include_once(THINKAPK_PATH.'config/bank.php');
			$this->assign('fqbank',$fqbank);
			$this->assign('banks_name',$bank_new);
			unset($fqbank,$bank_new,$bank);

		}
		/* 购买该商品可以得到多少钱的红包 */
		/*
		if ($goods ['bonus_type_id'] > 0) {
			$goods ['bonus_money']=D('BonusType')->getBonus($goods['bonus_type_id'],1);
			if ($goods ['bonus_money'] > 0) {
				$goods ['bonus_money'] = printf('%01.2f',$goods ['bonus_money']);
			}
		}
		*/
		
		//商品对应分类的销量排行
		$sales_top=D('SalesTop')->get('makeselltop',$goods['cat_id']);
		if(empty($sales_top)){
			$CM=new CategoryModel();
			$parent_cat_id=$CM->getParentId($goods['cat_id']);
			$sales_top=D('SalesTop')->get('makeselltop',$parent_cat_id);
		}
		if($sales_top) $this->assign('makeselltop',$sales_top);
		unset($CM,$sales_top);
		
		//商品描述
		$desc_items=D('GoodsDescItem')->getGoodsdesc($goods_id);
		if($desc_items) $this->assign('desc_item',$desc_items);
		unset($desc_items);
		
		//随机推荐商品，如：精品推荐，同价位商品 等
		$digg=D('GoodsDigg')->getGoods($goods_id);
		if($digg) $this->assign('diggs',$digg);
		unset($digg);
		//左边分类
		$this->assign('show_child',false);
		$this->setLeftCategory($goods['cat_id']);

		$this->assign('goods',$goods);
		
		$this->setTitle("{$goods['goods_name']}-{$goods['goods_maidian']}");
		$this->setKeywords($goods['keywords']);
		$this->setPosition($goods['cat_id'], $goods['goods_name']);
		$desc=$goods['goods_brief'].$goods['goods_maidian'];
		$this->setDescription(sub_str($desc,200));
		unset($desc,$goods);
		$this->display();
	}
	
	/*
	 * 改变属性、数量时重新计算商品价格
	*/
	/*
	public function changePrice(){
		$goods_id=is_numeric($_GET['goods_id'])?$_GET['goods_id']:0;
		if(!$goods_id) $this->error('商品ID不正确');
		
		$attr_id = isset ( $_GET['attr'] ) ? explode ( ',', $_GET['attr'] ) : array ();
		$number = (isset ( $_GET['number'] )) ? intval ( $_GET['number'] ) : 1;
		if ($number == 0) {
				$res ['qty'] = $number = 1;
		} else {
				$res ['qty'] = $number;
		}
		$goods_info = D('Goods')->getGood( $goods_id );
		//所选商品属性总价格
		$attr_price_total=D('GoodsAttr')->specPrice($attr_id);
		//商品多购折扣价格
		$discount_price=D('GoodsDiscount')->getGoodsDiscount($goods_id,$number)
		
		$goods_info ['promote_price_org'] = $goods_info ['promote_price_org'] - $discount_price;
		$goods_info ['rank_price'] = $goods_info ['rank_price'] - $discount_price;
		$shop_price = ($goods_info ['promote_price_org'] > 0) ? $goods_info ['promote_price_org'] : $goods_info ['rank_price'];
		//商品促销价
		$promote_price = 0
		if($goods_info['promote_price']!=0){
			$now_time=time();
			if($now_time >=$goods_info['promote_start_date'] && $now_time <= $goods_info['promote_end_date']){
				$promote_price=$goods_info['promote_price'];
			}
		}
		if ($goods_info['promote_p'] > 0 &&  $promote_price){
		   $shop_price =  $promote_price;
		}
		if($goods_info['is_promote_qc']){
				$shop_price = $goods_info['promote_p'];
		}
		$shop_price += $attr_amount;
		$ts = '';
		if($goods_info['pay_price']){
				$prices = round($shop_price*($GLOBALS['_CFG']['free_rate'] / 100)) * $number);
				$discount = round($shop_price*($GLOBALS['_CFG']['free_rate'] / 100))* $number;
				$ts = sprintf($_LANG['pay_to_price'], $prices);
		}
			$res ['result'] = price_format ( $shop_price * $number , false);
			$fq_money = array();
			$fq_money = fq_money ($shop_price * $number);
			foreach ( $fq_money as $key=>$val ) {
				$total_price = ((get_goods_fq_price ($goods_id,$key) + $shop_price)*$number - $discount)/$val['number'];
				$total_price = number_format ( $total_price, 2, '.', '' );
				$fq_money [$key] ['text'] = sprintf ( $GLOBALS ['_CFG'] ['currency_format'], $total_price ) .' x '.$val['number'];
			}
			$res['fqtext'] = $fq_money;
			$res['ts'] = $ts;
		}
	}
	*/
	/*
	 *　ajax商品的用户讨论区贴子列表
	*/
	public function getPosts(){
		$goods_id=is_numeric($_GET['goods_id'])?$_GET['goods_id']:0;
		if(!$goods_id) $this->error('商品ID不正确');
		$limit=is_numeric($_GET['limit'])?$_GET['limit']:15;
		$PM=new PostsModel();
		$total=$PM->getTotalPosts($goods_id);
		if($total){
			$posts=D('posts')->getPostsList($goods_id,$limit);
			$this->success($total,array('data'=>$posts));
		}else{
			$this->error('此商品暂时还没有讨论贴子');
		}
	}
	
	/*
	 * ajax商品评价列表
	*/
	public function getGoodsReview(){
		$goods_id=is_numeric($_GET['goods_id'])?$_GET['goods_id']:0;
		if(!$goods_id) $this->error('商品ID不正确');
		$limit=is_numeric($_GET['limit'])?$_GET['limit']:5;
		
		$reviews=D('GoodsReviews')->getGoodsReview($goods_id,$limit);
		if($reviews) $this->assign('reviews',$reviews);
		$this->assign('goods_id',$goods_id);
		$html=$this->fetch('/common:goods_review_list');
		$this->success($html);
	}
	/*
	 * ajax商品评价汇总
	*/
	public function getReviewSummary(){
		$goods_id=is_numeric($_GET['goods_id'])?$_GET['goods_id']:0;
		if(!$goods_id) $this->error('商品ID不正确');
		
		$review_summary=D('GoodsReviews')->getGoodsAssess($goods_id);
		if($review_summary){
			$this->success('',$review_summary);
		}else{
			$this->error('此商品暂没有评价');
		}
	}
	
	/*
	 *ajax取商品描述内容
	*/
	public function getGoodsDesc(){
		$goods_id=is_numeric($_GET['id'])?$_GET['id']:0;
		if(!$goods_id) $this->error('描述ID不正确');
		
		$desc=D('GoodsDescItem')->where("id={$goods_id}")->getField('item_desc');
		$this->success($desc);
	}
	
	/*
	 * ajax取商品关联的广告
	*/
	public function getAds(){
		$goods_id=is_numeric($_GET['goods_id'])?$_GET['goods_id']:0;
		if(!$goods_id) $this->error('商品ID不正确'.$_GET['goods_id']);
		$goods_ad=($_GET['goods_ad']=='' || $_GET['goods_ad']=='||')?'':trim($_GET['goods_ad']);
		if($goods_ad=='') $this->error('商品没有关联广告');
		
		$array = array ();
		$ids = explode ( "|", $goods_ad );
		$ids1 = explode ( ",", $ids [0] );
		$ids2 = explode ( ",", $ids [1] );
		$ids3 = explode ( ",", $ids [2] );
		$GAM=new GoodsAdsModel();
		if ($ids1 [0]) {
			$ad=$GAM->getAd($ids1[0]);
			if($ad) $array['ads1']=$ad;
			else $array['ads1']=array();
		} else {
			$array ['ads1'] = array ();
		}
		if ($ids2 [0]) {
			$ad=$GAM->getAd($ids2[0]);
			if($ad) $array['ads2']=$ad;
			else $array['ads2']=array();
		}else{
			$array['ads2']=array();
		}
		if ($ids3) {
			$ad=$GAM->getAd($ids3[0]);
			if($ad) $array['ads3']=$ad;
			else $array['ads3']=array();

		} else {
			$array ['ad3'] = array ();
		}
		if(empty($array)) $this->error('此商品没有广告关联');
		else $this->success('',$array);
	}
	
	/*
	 *ajax记录商品历史浏览，并加载显示
	*/
	public function loadHistory(){
		$goods_id=is_numeric($_GET['goods_id'])?$_GET['goods_id']:0;
		$now_time=time();
		$history=$_COOKIE['ShiHui']['history'];

		if(empty($history)){
			setcookie('ShiHui[history]',$goods_id,$now_time+3600*24*30);
			$history=$goods_id;
		}else{
			$array=explode(',',$history);
			array_unshift($array,$goods_id);
			$array=array_unique($array);
			if(count($array)>10) $array=array_slice($array,0,9);
			$history=implode(',',$array);
			setcookie('ShiHui[history]',$history,$now_time+3600*24*30);	
		}
		$this->assign('history',D('Goods')->getHistory($history));
		$html=$this->fetch('/common:history');
		$this->success($html);
	}
	/*
	 *ajax清空商品历史浏览记录
	*/
	public function clearHistory(){
		setcookie('ShiHui[history]','',-100);
		$this->success('历史浏览记录已清空');
	}
	
	//ajax取商品限时报购时间差
	public function getPromoteDiff(){
		$goods_id=is_numeric($_GET['goods_id'])?$_GET['goods_id']:0;
		if(!$goods_id) $this->error('商品ID不正确');
		$promote_info=D('Goods')->field('is_promote,promote_start_date,promote_end_date')->where("goods_id={$goods_id}")->find();
		$diff=0;
		if($promote_info['is_promote']==1){
			$now_time=time();
			if($now_time>$promote_info['promote_start_date'] && $now_time<$promote_info['promote_end_date']){
				$diff=$promote_info['promote_end_date']-$now_time;
			}
		}
		$this->success($diff);
	}
}
?>